import csv, sqlite3
import getopt
import sys

#C:\Users\ikbhal\mycrawling\incrediblepeople
#conn = sqlite3.connect("c:\\users\ikbhal\\mycrawling\\watchcartoononline\\wco.sqlite")
#conn = sqlite3.connect("c:\\users\ikbhal\\mycrawling\\torrentzap\\torrentzap.sqlite")
#conn = sqlite3.connect("c:\\users\ikbhal\\mycrawling\\limetorrents\\limetorrents.sqlite")
#conn = sqlite3.connect("c:\\users\ikbhal\\mycrawling\\incrediblepeople\\scientits.incredible-people.sqlite")
#
#sqlstatement = "SELECT * from wco limit 1"
#sqlstatement = "SELECT episodeTitle from wco"
#sqlstatement = "SELECT count(*) from wco"
#sqlstatement = "SELECT title from  torrentzap "
#sqlstatement = "SELECT count(*) from  torrentzap limit 1"
#sqlstatement = "SELECT * from  torrentzap limit 1"

#sqlstatement = "SELECT count(*) from  limetorrents limit 1"
#sqlstatement = "SELECT * from  limetorrents limit 1"
#sqlstatement = "SELECT distinct url from limetorrents "
#sqlstatement = "SELECT distinct url from torrentzap"
sqlstatement = "SELECT count(distinct url) from limetorrents "
#sqlstatement = "SELECT count(distinct url) from torrentzap "


version = '1.0'
db ="c:\\users\ikbhal\\mycrawling\\limetorrents\\limetorrents.sqlite"
query ="SELECT count(distinct url) from limetorrents "


print 'ARGV      :', sys.argv[1:]
options, remainder = getopt.gnu_getopt(sys.argv[1:], 'd:q:r', ['db=',
                                                             'query=',
                                                             'removeTable='
                                                             'version=',
                                                             ])
print 'OPTIONS   :', options
removeTable =""
for opt, arg in options:
    if opt in ('-d', '--db'):
        db = arg
    elif opt in ('-q', '--query'):
        query = arg
    elif opt in ('-r', '--removeTable'):
        removeTable = arg
    elif opt == '--version':
        version = arg

print 'VERSION   :', version
print 'DB    :', db
print 'QUERY    :', query
print 'REMOVETABLE   :', removeTable
print 'REMAINING :', remainder

conn = sqlite3.connect(db)
curs = conn.cursor()
sqlstatement =query
curs.execute(sqlstatement)
if(removeTable == ""):
    exit
records = curs.fetchall()

for rec in records:
	print rec